% %
% LAYOUT_E.TEX - Short description of REFMAN.CLS
%                                       99-03-20
%
%  Updated for REFMAN.CLS (LaTeX2e)
%
\documentclass[twoside,a4paper]{refart}
\usepackage{makeidx}
\usepackage{ifthen}
\usepackage{amsmath}
\usepackage{graphicx}
% ifthen wird vom Bild von N.Beebe gebraucht!

\def\bs{\char'134 } % backslash in \tt font.
\newcommand{\ie}{i.\,e.,}
\newcommand{\eg}{e.\,g..}
\DeclareRobustCommand\cs[1]{\texttt{\char`\\#1}}

\title{DayTrader -- A Programming Language For Traders\\Reference Manual}
\author{Tsung-Kai Lin(tl2450)\\
Jie Huang(jh3105)\\
Fang Da(fd2263)\\
Ran Wang(rw2409)\\
Shao-Chuan Wang(sw2644)\\
Department of Computer Science, Columbia University}

\date{}
\emergencystretch1em  %

\pagestyle{myfootings}
\markboth{\textrm{DayTrader} -- A Programming Language For Traders}%
         {\textrm{DayTrader} -- A Programming Language For Traders}

\makeindex 

\setcounter{tocdepth}{2}

\begin{document}

\maketitle


\newpage
\tableofcontents

\newpage


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Introduction}
DayTrader programming language aims at automatic day trading. 
The goal of DayTrader is to abstract the selection of the target stocks/options and the discovery of the indicators. 
We make the selection of target stocks and their calculation fairly easy. 
With DayTrader, the user can write just a few lines to complete an auto-trading program.  
The following is a simple hello world example:
\subsection{Hello World Example}

@Buy\\
select all\\
sector == “Technology”\\
Math.round(volume) $>$ get\_property("MSFT", "volume")\\
price $>$ get\_price\_at(current, Datetime(2008, 3, 2)) * 80\%\\
MA(15) crossbelow MA(30)\\
buy 100\\

@Sell\\
profit $>$ 5\% or loss $<$ -5\%\\
sell\\\\
We will detail the syntax in the following sections.
\section{Programming Model}
The design of DayTrader language is based on two models. One is the model of buy and sell engines, which serves as two daemon processes to periodically run user-defined strategy to make decision on buying and selling stocks/options. The other is the pipeline model, which abstracts the process of selecting the stocks to be traded within a strategy.
\subsection{Buy/Sell Engine Model}
Each instance of the DayTrader executables consist of two strategies: a buy strategy and a sell strategy, which is denoted by the keyword @Buy and @Sell.

The buy engine is the daemon process to run the buy strategy at every market tick until the conditions defined by it are met and thus a ‘buy’ action is triggered. After that, the buy engine is stopped and the sell engine is instead launched.

Like the buy engine, the sell engine runs the sell strategy at every market tick. 
But it will only stop when all the stocks and options in the user’s portfolio are sold.
Then, the buy/sell cycle will restart from the beginning. Following is a chart explaining the buy and sell cycles:
\begin{figure}[h!]
\centering
\includegraphics[scale=0.45]{pic}
\caption{Programming Model of DayTrader}
\end{figure}
This design is based on the nature of the day trading. Stocks and options purchased are usually sold in just several minutes or even seconds. 
If the conditions of the sell strategy has never been met by the market close time of the day, the stocks(or options) will be forcefully sold out by the system.

\subsection{Pipeline Model}
The pipeline model abstracts the process of selecting a subset of stocks (or stock options) meeting certain conditions.

As mentioned, a typical DayTrader program consists of one `buy' strategy and one `sell' strategy.
Each sentence of the code of the strategies can be viewed as a filter narrowing down the set of stocks which meet the conditions defined by the current sentence of the code and all the previous sentences.

The Pipleline Model defines an implicit input and output for each sentence of the code, while the output of one sentence is the input of the next. Either the input and output contains a boolean value and a set of stocks, illustrated by the following:
\\\\\hspace*{20pt}
\{true$|$ \{MSFT, GOOG, DELL\}\}
\\\\
The boolean value usually indicates the overall logical result of the sentence, and the set contains all the stocks left after being filtered by all the previous code in the strategy. 
If any of the sentences of a strategy returns false for the boolean value, the whole strategy returns immediately, and for most of the cases the engine will restart the strategy from the beginning at the next tick. (So, the input boolean value for a sentence is always true.)

The conditions defined by a code sentence is tested against each stock (or stock option) in the input set. 
If any of the conditions are not met for a stock, the stock is removed from the set. And the resulting set will be passed as the output set to the next sentence. 
For example, for the following sentence, only the stocks in the input set with growth rate higher than 10\% will be passed to the next sentence.
\\\\\hspace*{20pt}
growth $>$ 10\% 
\\\\
The input boolean value for the first sentence of either the buy or the sell strategy is `true'. 
The input set of stocks for the first sentence is the set of all the stocks in the markets for the buy strategy, and is the set of all stocks (or stock options) currently hold for the sell strategy.

Users of DayTrader need to understand the Pipline model and keep that in mind when they write the strategy code. 
But for most of the cases, they do not need to directly operate on the pipeline inputs and outputs.

The following snippet shows how the implicit pipleline output might be for our `Hello World' program:
\begin{align*}
&\text{@Buy}&\\
&\text{select all}&\text{//  \{true | \{all stocks in the market\}\}} \\
&\text{sector == ``Technology''} & \text{ //  \{true | \{all stocks in the ‘Technology” sector\}\}}\\
&\text{MA(15) crossbelow MA(30)}&\text{ //  \{true | \{MSFT, GOOG\}\}}\\
&&\text{(say, only MSFT and GOOG meet the condition)} \\
&\text{buy 100}&\text{ //  \{true | \{MSFT, GOOG\}\}}\\\\
&\text{@Sell}&\\
&\text{select all}&\text{//  \{true | MSFT, GOOG} \\
&\text{profit $>$ 5\% $||$ profit $<$ -5\%} &\text{//   {false | {}}  (say, none meets the condition) }\\
&\text{sell}&
\end{align*}


\section{Lexical Conventions}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Characters set}
Our programming language uses 7-bit ASCII code (0-127) as our characters set.
\subsection{Comments}
// this is comments
\subsection{Identifiers}
An identifier in DayTrader is a sequence of letters, digits, and underscore, and it must start with a letter or underscore. 
In terms of regular expression, an identifier is [a-zA-Z][0-9a-zA-Z]*
Some identifiers are reserved as keywords (stated below), and may not be used as identifiers.
\subsection{Keywords}
\begin{enumerate}
\item Grammer Keywords
	\begin{itemize}
	\item print
	\item and, or
	\item if, else
	\item current
	\item true, false
	\item select 
	\item all
	\item buy, sell
	\item call, put
	\item current
	\item @Buy, @Sell
	\end{itemize}
\item Domain Specific Keywords
\begin{enumerate}
\item Stock Fields Keywords
	\begin{itemize}
	\item capital - Market Capital
	\item peratio - P/E Ratio
	\item divyield - Dividend and Yield
	\item open - Open Price
	\item preclose - Previous closing price
	\item volume - Today's volume
	\item avgvol - Average daily volume
	\item price - Price
	\item sector - Sector
	\end{itemize}
\item Market Global Attributes
	\begin{itemize}
	\item INDEX - Current index
	\item INDEXO - Index Open
	\item INDEXC - Index Close
	\item INDEXH - Index High
	\item INDEXL - Index Low
	\item INDEXV - Index Volume
	\item INDEXA - Index Amount 
	\item INDEXADV - Number of advanced companies
	\item INDEXDEC - Number of declined companies
	\end{itemize}
\item balance - Current balance in account
\item profit - Current profit of a stock being held
\item current - The current stock (string)
\end{enumerate}
\end{enumerate}

\subsection{Constants}
\begin{itemize}
\item NUM\_MAX\_VALUE
\item NUM\_MIN\_VALUE
\item INT\_MAX\_VALUE
\item INT\_MIN\_VALUE
\item VOL\_PER\_UNIT       // Volume per Unit, 100 for the New York exchanges
\end{itemize}

\subsection{Data Type}
DayTrader uses an implicit and static typing system. 
Here is a list of data types. The rest of this section is the explanation of each of these data types.
\begin{itemize}
	\item Numerical
	\item Boolean
	\item String
	\item Data Stream
	\item Datetime/Datespan
\end{itemize}

\subsubsection{Numerical}
We only provide one numerical data type. 
Unlike many other programming languages, we make no difference between integers and decimals, nor do we distinguish short, long, float and double. 
All numerical values simply belongs to one single type.\\\\
Relevant constants:
	\begin{itemize}
	\item NUM\_MAX\_VALUE (3.40282346638528860e+38)
	\item NUM\_MIN\_VALUE (1.40129846432481707e-45)
	\end{itemize}
\subsubsection{Boolean}
There are two literals of boolean type: true and false. The following operators operate on
boolean values:
	\begin{enumerate}
	\item not operator\\
	Logical negation
	\item ==, != operator\\
	Equal, Not-Equal
	\item and or operator\\
	Logical ``and'' and ``or''
	\end{enumerate}
\subsubsection{String}
String is a type consisting of a series of ASCII characters. 
DayTrader supports string literals and read-only string manipulations. 
String is an important type used in indexing different stocks, selecting attributes and streaming out data on standard output.

For all functions and indicator methods, string representation of Stocks is used as default form arguments.

\subsubsection{Data Stream}
Data stream type is the type returned (typically) by an indicator, containing the trend of the indicator values over a period of time.
Data of this type wrap a whole sequence of values to provide a more complete picture of how the values have been changing over time.
This type is the only valid type for operator crossover and crossdown. No other operations can be performed.

Note that some indicators can return both Numerical type and Data stream type, depending on whether it is present in a crossover/crossdown operation or numerical (comparative, arithmetic etc) operation.
\subsubsection{Datetime}
Datetime type represents a particular point of time. Datetime type has following attributes:
	\begin{itemize}
	\item Year\\
	      Numerical type, with possible value from 1792 - INT\_MAX\_VALUE
	\item Month\\
	      Numerical type, from 1 to 12
	\item Day\\
	      Numerical type, from 1 to 31
	\item WeekDay\\
              Enum type, from Sunday to Saturday
	\item Hour\\
	      Numerical type, from 0 to 23
	\item Minute\\
	      Numerical type, from 0 to 59
	\item Second\\
	      Numerical type, from 0 to 59
	\end{itemize}
Example - Get the quote of a stock at a particular time:\\\\
quote = GetQuote(MSFT, Datetime(2010, 3, 2, 12, 23, 30))\\
/* Get the quotes of Microsoft at 2010/3/2 12:23:30 */\\\\
\subsubsection{Timespan}
Timespan type is used to represent a period of time. It has the following attributes:
	\begin{itemize}
	\item Year\\
	      Numerical type, [-INT\_MIN\_VALUE, INT\_MAX\_VALUE]
	\item Month\\
	      Numerical type, [-11, 11]
	\item Day\\
	      Numerical type, [-365, 365]
	\item Hour\\
	      Numerical type, [-23, 23]
	\item Minute\\
	      Numerical type, [-59, 59]
	\item Second\\
	      Numerical type, [-59, 59]
	\item TotalMonths, TotalWeeks, TotalDays, TotalHours, TotalMinutes, TotalSeconds\\
	      Numerical type, [-INT\_MIN\_VALUE, INT\_MAX\_VALUE]

	\end{itemize}
\subsection{Operators}
\begin{enumerate}
\item Arithmetic\\
	\begin{enumerate}
	\item The additive operators(+, -)\\
	Additive operators can only used in the numerical, Datetime and timespan data type. + and - are left associative. 
	The result of the + and - operators are the sum and substraction of the operands, respectively.
	\item The multiplicative operators(*, /, \%)\\
	Multiplicative operators can only used in the numerical data type. * , / and \% are left associative. 
	The result of the * and / operators are the product, division and of the operands, respectively. 
	The result of the \% operator yields the remainder from the division of the ﬁrst operand by the second operand. 
	They have higher precedence than additive operators.
	\end{enumerate}
\item Relational ($>$, $<$, $<=$, $>=$)\\
Relational operators can only be used in the numerical, Datetime and timespan data type.
The operators $<$ (less than), $>$ (greater than), $<=$ (less than or equal to) and $>=$ (greater than or equal to) all yield 0 if the specified relation is false and 1 if it is true.
\item Equality (==)\\
The == (equal to) operator is exactly analogous to the relational operators except for their lower precedence.
\item Logical (and, or, not)\\
The `and' operator returns 1 if both its operands are non-zero, 0 otherwise. The `or' operator returns 1 if either of its operands is non-zero, and 0 otherwise.

\end{enumerate}

\section{Language Syntax}
\subsection{Control Flow}
As discussed in the `Programming Model' section, DayTrader uses pipeline model as the language logical flow. 
This design makes normal language control flows (like `for', `while', `if') not necessary for writing a trading strategy in DayTrader. 
For a typical statement of the strategy, if mapped to languages like Java, it would be similar to the following:\\\\
for each stock \{\\
\hspace*{20pt}if (!statement) remove stock\\
\}

\subsection{Variable assignment}
For the kernel features of our implementation, we do not support user-defined or user-decleared variables. 
This is a novel design appropriate for real-time process based language. 
Since there is no variable used in the language, there is no use of variable assignment either.
\subsection{Scope}
Similar to the case of variable assignment, the DayTrader does not have concept of variable scope because the language does not support user defined variables.
\subsection{Function Definition}
DayTrader provides functions for general calculation and stock selection in the trading market. 
There are two types of functions: generic function, set function and property function. 
\subsubsection{Generic Function}
Generic function is a normal function like many other programming languages, such as mathematical functions floor(x) and ceiling(x). 
All generic functions are associated with a function name-space. 
For example, floor(x) and ceiling(x) are in Math name-space. 
To invoke them, one has to write Math.floor(x) and Math.ceiling(x) to avoid name conflict between function names.
\\\\
\hspace*{20pt}Math.ceiling(1.5)\\
\hspace*{20pt}Math.floor(2.0)

\subsubsection{Set Function}
Unlike the generic function, set function is applied to stocks in the pipeline. 
For example, most of indicator functions are set functions. 
They are implicitly applied to stocks in the pipeline.\\\\
\hspace*{20pt}MA(5) crossover MA(15)
\\\\
In the above example, MA is a set function. This statement is to search the whole set to find out the subset of stocks whose the moving average (5 days) crossovers the moving average (15 days).
\subsubsection{Property Function}
In DayTrader, searching and obtaining information for certain stock is through a global method called ``getProperty()''. 
The formal argument of this function is the string name of a stock and its related property field. 
The ``getProperty()'' function will check the input arguments and call background data interfaces to conduct dynamic query for the user.

The procedure involved in ``getProperty()'' is as follows:
\begin{enumerate}
\item checking on validity of input string argument
\item calling background real-time data interface to dynamically search stock and related property fields.
\item do analysis on return values from data interfaces:
	\begin{enumerate}
	\item if return value is valid, go ahead return the value to DayTrader program
	\item if return value is ERROR information, generating run-time error exceptions.
	\end{enumerate}
Therefore, for invalid searching for stock and property information using ``getProperty()'', such as not existed stock name or void property field, there will be run-time error generated.	
\end{enumerate}
\subsection{Context-Free Grammar}
The grammar of DayTrader should be able to give users an idea of how to write a DayTrader program more rigorously. 
Terminals are denoted by capitalized letters, while non-terminals are denoted by lower case letters. \\\\
\hspace*{20pt}/* a program is constructed by strategies */\\
\hspace*{20pt}program $\rightarrow$ buysec strategy\\
\hspace*{50pt}$|$  buysec strategy sellsec strategy\\\\
\hspace*{20pt}buysec $\rightarrow$ BUY newline \hspace{30pt} /* BUY = @Buy */\\\\
\hspace*{20pt}sellsec $\rightarrow$ SELL newline\hspace{30pt} /* SELL = @Sell */\\\\
\hspace*{20pt}strategy $\rightarrow$ strategy linestat\\
\hspace*{50pt}$|$  linestat\\\\
\hspace*{20pt}linestat $\rightarrow$ statement newline\\
\hspace*{50pt}$|$  comment newline\\
\hspace*{50pt}$|$  newline\\\\
\hspace*{20pt}statement $\rightarrow$ printstat\\
\hspace*{50pt}$|$  selectstat\\
\hspace*{50pt}$|$  buystat\\
\hspace*{50pt}$|$  sellstat\\
\hspace*{50pt}$|$  condition\\\\
\hspace*{20pt}printstat $\rightarrow$ PRINT list\\\\
\hspace*{20pt}selectstat $\rightarrow$ SELECT list\\\\
\hspace*{20pt}buystat $\rightarrow$ BUY number\\\\
\hspace*{20pt}sellstat $\rightarrow$ SELL number\\
\hspace*{50pt}$|$  SELL\\\\
\hspace*{20pt}number $\rightarrow$ INTEGER\\
\hspace*{50pt}$|$  FLOAT\\
\hspace*{50pt}$|$  PERCENT\\\\
\hspace*{20pt}list $\rightarrow$ list , object\\
\hspace*{50pt}$|$  object\\\\
\hspace*{20pt}object $\rightarrow$ STRINGLITERAL /* string literal */\\
\hspace*{50pt}$|$  identifier\\
\hspace*{50pt}$|$  expr\\
\hspace*{50pt}$|$  function\\
\hspace*{50pt}$|$  VOLUME /* domain specific keywords */ \\
\hspace*{50pt}$|$  PRICE \\
\hspace*{50pt}$|$  CURRENT \\

\hspace*{20pt}expr $\rightarrow$ number\\
\hspace*{50pt}$|$  expr + expr\\
\hspace*{50pt}$|$  expr - expr\\
\hspace*{50pt}$|$  expr * expr\\
\hspace*{50pt}$|$  expr / expr \\
\hspace*{50pt}$|$  -expr \\
\hspace*{50pt}$|$  expr \^ expr \\
\hspace*{50pt}$|$  ( expr ) \\

\hspace*{20pt}identifier $\rightarrow$ IDENT . IDENT\\
\hspace*{50pt}$|$  IDENT\\

\hspace*{20pt}function $\rightarrow$ identifier ( list )\\

\hspace*{20pt}condition $\rightarrow$ condition LOGICAL condition /* LOGICAL = and | or */ \\
\hspace*{50pt}$|$  indicator CROSSUP indicator /* crossup = crossup | crossdown */\\
\hspace*{50pt}$|$  function compare object\\
\hspace*{50pt}$|$  SECTOR equal object\\
\hspace*{50pt}$|$  LOST compare object\\
\hspace*{50pt}$|$  PROFIT compare object\\
\hspace*{50pt}$|$  VOLUME compare object\\
\hspace*{50pt}$|$  PRICE compare object\\
\hspace*{50pt}$|$  identifier compare object\\

\hspace*{20pt}compare $\rightarrow$ == \\
\hspace*{50pt}$|$  $<$\\
\hspace*{50pt}$|$  $>$\\
\hspace*{50pt}$|$  $<=$\\
\hspace*{50pt}$|$  $>=$\\

\hspace*{20pt}indicator $\rightarrow$ CAPIDENT ( list )\\
\hspace*{50pt}/* CAPIDENT is used to represent capitalized identifier */ \\

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Additional Features}
\subsection{Enum Type}
Enum types are used to describe a list of enumeration values. They are defined using the enum keyword, and is referenced by the dot operator (.):
Sample build-in enum types:\\\\
\hspace*{20pt}enum Exchange:\\
\hspace*{40pt}AMEX\\
\hspace*{40pt}NASDAQ\\
\hspace*{40pt}NYSE\\\\
\hspace*{20pt}exchange = Exchange.NASDAQ\\\\
\hspace*{20pt}enum Sectors:\\
\hspace*{40pt}AllSectors\\
\hspace*{40pt}BasicMaterials\\
\hspace*{40pt}CapitalGoods\\
\hspace*{40pt}Conglomerates\\
\hspace*{40pt}ConsumerCyclical\\
\hspace*{40pt}ConsumerNonCyclical\\
\hspace*{40pt}Financial \\
\hspace*{40pt}HealthCare\\
\hspace*{40pt}Service\\
\hspace*{40pt}Technology\\
\hspace*{40pt}Transportation\\
\hspace*{40pt}Utilities\\
\subsection{Option}
Option is another common trading commodity. 
Instead of buying the stock itself, option buys/sells (or in trading terminology, put/call) the contracts to sell a stock at a certain price at a certain date. 
A stock option should provide the following information:
\begin{itemize}
\item Stock - The stock bought
\item Price - The price when purchased
\item Amount - The amount purchased
\item Time - Datetime type, the time of purchased
\item ExecutePrice - The excute price option contract
\item ExecuteDate - Datetime type, The execute date of the option contract
\end{itemize}
\subsection{Multiple Markets}
We now only support NASDAQ Exchange. 
A additional feature is to support multiple Markets. 
A reasonable and feasible extension is to include the three major exchanges in New York shown as follows:
\begin{itemize}
\item NYSE - New York Stock Exchange
\item NASDAQ - NASDAQ Stock Exchange
\item AMEX - American Stock Exchange
\end{itemize}
Following are the keywords which should be specific to a market after we extend DayTrader to multiple markets:
\begin{itemize}
\item INDEX - Current index
\item INDEXO - Index Open
\item INDEXC - Index Close
\item INDEXH - Index High
\item INDEXL - Index Low
\item INDEXV - Index Volume
\item INDEXA - Index Amount 
\item INDEXADV - Number of advanced companies
\item INDEXDEC - Number of declined companies
\end{itemize}
Market specific information is referenced by specifying the market name, illustrated as following:\\\\
\hspace*{20pt}NYSE.INDEX $>$ 20
\subsection{Custom Function}
Custom function is another feature we consider to implement in future efforts. 
Advanced users are able to implement custom designed indicators or other general functions. 
This will enable the user to develop tools associated with special trading techniques. 
All files with extension .func will be automatically imported.  

As writing user-defined functions, a name-space is implemented as a java class. The following is an example of custom-defined general function:

math.func\\
class Math \{\\

\hspace*{20pt}public static double floor(double x);\\
\hspace*{20pt}public static double ceiling(double x);\\
\}
\\\\
For custom indicators, we use MA as an example. 
All indicators have to implement the following interface:
\\\\
public interface SetFunctionInterface \{\\
\hspace*{20pt}public TickData[] calculate(TickData[] data);\\
\}\\\\

Here is the sample indicator code:\\
macd.func\\
//set method, using for indicators in stock selection\\
class MA implements SetFunctionInterface \{\\
\hspace*{20pt}int days;\\
\hspace*{20pt}public MA(int days) \{  this.days = days; \}\\
\hspace*{20pt}public TickData[] calculate(TickData[] data) \{\\
\hspace*{40pt}if (data.length() $<$ this.days) \{\\
\hspace*{60pt}System.err.println('[MA] TickData is insufficient for calculation.');\\
\hspace*{40pt} \}\\
\hspace*{40pt}TickData[] avgData = new TickData[data.length() - this.days];\\
 \hspace*{40pt}double sum = 0.0;\\
 \hspace*{40pt}for (int i = 0;i $<$ this.days; i++) \{\\
 \hspace*{60pt}sum += data[i].close;\\
 \hspace*{40pt}\}\\
 \hspace*{40pt}avgData[0] = sum / days;\\
 \hspace*{40pt}for (int j = this.days; j $<$ data.length(); j++) \{\\
 \hspace*{60pt}sum = sum - data[j - this.days] + data[j];\\
 \hspace*{60pt}avgData[j - this.days] = sum/days;\\
 \hspace*{40pt}\}\\
 \hspace*{40pt}return avgData;\\
 \hspace*{20pt}\}\\
\}
\printindex

\end{document}
